<template>
    <div class="background">
        <el-card>
            <h3>用户注册</h3>
            <el-form
                ref="form"
                :mode="form"
                label-position="right"
                label-width="70px"
            >
                <el-form-item label="用户名">
                    <el-input
                        v-model="form.username"
                        prefix-icon="el-icon-user-solid"
                        placeholder="请输入用户名"
                    ></el-input>
                </el-form-item>

                <el-form-item label="密码">
                    <el-input
                        type="password"
                        v-model="form.password"
                        prefix-icon="el-icon-s-order"
                        placeholder="请输入密码"
                    >
                    </el-input>
                </el-form-item>

                <el-form-item label="年龄">
                    <el-input
                        type="text"
                        v-model.number="form.age"
                        prefix-icon="el-icon-s-order"
                        placeholder="请输入年龄"
                    >
                    </el-input>
                </el-form-item>

                <el-form-item label="电话">
                    <el-input
                        type="text"
                        v-model="form.phone"
                        prefix-icon="el-icon-s-order"
                        placeholder="请输入电话"
                    >
                    </el-input>
                </el-form-item>

                <el-form-item label="性别">
                    <div></div>
                    <el-dropdown @command="handleCommand">
                        <span
                            class="el-dropdown-link"
                            :style="{ color: form.sex ? '#606266' : '#ccc' }"
                        >
                            {{ form.sex ? form.sex : "请选择性别"
                            }}<i class="el-icon-arrow-down el-icon--right"></i>
                        </span>
                        <el-dropdown-menu slot="dropdown">
                            <el-dropdown-item :command="1">男</el-dropdown-item>
                            <el-dropdown-item :command="0">女</el-dropdown-item>
                        </el-dropdown-menu>
                    </el-dropdown>
                </el-form-item>

                <el-form-item class="login-footer"> </el-form-item>
            </el-form>

            <div class="form-footer">
                <el-button @click="handleRegister" type="primary"
                    >注册</el-button
                >
            </div>
        </el-card>
    </div>
</template>

<script>
import { userRegister } from "../api/index";
import {
    regularName,
    regularPassword,
    regularAge,
    regularTel,
    regularSex,
} from "../rules";

export default {
    name: "UserResgiser",
    data() {
        return {
            form: {
                username: "",
                password: "",
                age: "",
                phone: "",
                sex: "",
                avatar: ``,
            },
        };
    },
    methods: {
        async handleRegister() {
            let { username, password, phone, sex, age } = this.form;
            sex = sex === "男" ? 1 : 0;

            const infoMap = {
                username: "用户名",
                password: "密码",
                age: "年龄",
                phone: "电话号码",
                sex: "性别",
            };

            const validationRule = {
                username: () => regularName.test(username),
                password: () => regularPassword.test(password),
                age: () => regularAge.test(age),
                phone: () => regularTel.test(phone),
                sex: () => regularSex.test(sex),
            };

            for (let key in validationRule) {
                if (!validationRule[key]()) {
                    this.$message.error(`${infoMap[key]}格式错误，请重试`);
                    return;
                }
            }

            const res = await userRegister(
                "/user/registry",
                JSON.stringify({ ...this.form, sex })
            );

            if (res.data.code === 0) {
                this.$message({
                    message: "注册成功，请登录",
                    type: "success",
                });

                this.$router.push({ name: "login" });
            } else {
                this.$message.error(res.data.msg);
            }
        },

        handleCommand(command) {
            if (command === 1) {
                this.form.sex = "男";
            } else if (command === 0) {
                this.form.sex = "女";
            }
        },
    },
};
</script>

<style lang="less" scoped>
.background {
    height: 100vh;
    background-image: url("../assets/backimg.png");
    background-repeat: no-repeat;
    background-attachment: fixed;
    background-size: cover;
    position: relative;

    .el-card {
        width: 500px;
        position: absolute;
        top: 200px;
        left: 50%;
        transform: translateX(-200px);
        border-radius: 20px;

        h3 {
            text-align: center;
        }

        .login-footer {
            width: 100%;
            display: flex;
            justify-content: space-around;
        }

        .el-dropdown-link {
            border: 1px solid #dcdfe6;
            padding: 8px 15px;
            border-radius: 5px;
            color: #606266;
        }

        .form-footer {
            display: flex;
            justify-content: center;
            margin-top: 50px;
        }
    }
}
</style>
